<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
    <meta content="featon inc." name="author">
    <link href="/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/vendor/skin/ftskin.css" rel="stylesheet">
    <title>添加(更新)产量记录</title>
    <style>
    </style>
</head>

<body style="margin-bottom: 60px;">

<div class="ft-subpage-wrapper container-fluid">
    <form action="#" id="form1" method="post" name="form1">
        <div class="row">
            <div class="col-lg-12" id="form_table">
                <fieldset>
                    <legend id="titleText">--</legend>
                    <table cellpadding="0" cellspacing="0" class="form-table form-table-border" id="">
                        <tr style="display: none">
                            <th><span class="f_red">*</span>产品批号</th>
                            <td colspan="3">
                                <input class="form-control" id="productNo" name="productNo" placeholder="" type="text"
                                       readonly
                                       value="">
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>产品名称</th>
                            <td colspan="3">
                                <select class="form-control" id="cpId" name="cpId">
                                    <option disabled id="cpId_val" selected>--请选择--</option>
                                </select>
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>产量</th>
                            <td colspan="3">
                                <input class="form-control" id="scVal" name="scVal" placeholder="" type="number"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>生产日期</th>
                            <td colspan="3">
                                <input class="form-control" id="scDate" name="scDate" placeholder=""
                                       type="datetime-local"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th>备注</th>
                            <td colspan="3">
                                <input class="form-control" id="remark" name="remark" placeholder="" type="text"
                                       value="">
                            </td>
                        </tr>
                    </table>
                </fieldset>
                <div class="bottom-tools-box">
                    <button class="btn btn-info" id="save_but" onclick="addAndUpdateObject()" type="button"><span
                            aria-hidden="true" class="glyphicon glyphicon-floppy-disk"></span> 保存
                    </button>
                    <button class="btn btn-warning" onclick="top.tab_colseOnclick();" type="button"><span
                            aria-hidden="true" class="glyphicon glyphicon-remove-circle"></span> 取消
                    </button>
                </div>

            </div>
        </div>
    </form>
</div>
<input th:id="id" th:value="${id}" type="hidden"/>

<script src="/static/vendor/jquery/jquery-3.3.1.min.js"></script>
<script src="/static/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/vendor/skin/js/ft-ui.js"></script>

<script>
    $("form input,form select").blur(function () {
        let siblings = $(this).siblings(".cError");
        $(siblings).empty();
        if ($(this).val().trim() === "" || $(this).val() === null) {
            $(siblings).append(`
            <span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>
            `)

            if ($(this).attr("id") !== "remark") {
                $(this).focus();
            }
        }
    })

    function iniInfoForm() {
        let validator = $("#form1").validate({
            debug: true,
            rules: {
                // user_realname: {
                //     required: true
                // },
                // dq_yw_name: {
                //     required: true
                // }
            },
            messages: {
                // user_realname: {
                //     required: "执法人员不能为空！"
                // },
                // dq_yw_name: {
                //     required: "所属地区不能为空"
                // }
            },
            errorPlacement: function (error, element) {
                error.appendTo(element.parents('td').find(".cError"));
            },
            submitHandler: function (form) {
            }
        });
    }

    let id = $("#id").val();
    $(function () {
        function p(s) {
            return s < 10 ? '0' + s : s;
        }

        function GetNowTime() {
            let myDate = new Date();
            let year = myDate.getFullYear();
            let month = myDate.getMonth() + 1;
            let date = myDate.getDate();
            let h = myDate.getHours();       //获取当前小时数(0-23)
            let m = myDate.getMinutes();     //获取当前分钟数(0-59)
            let s = myDate.getSeconds();
            return year + '-' + p(month) + "-" + p(date) + "T" + p(h) + ':' + p(m) + ":" + p(s);
        }

        $("#scDate").val(GetNowTime());
        if (id !== null && id !== '') {
            $.get("/py/getObjByArgs?id=" + id, function (result) {
                console.log(result)
                $("#productNo").val(result[0].productNo);
                $("#cpId_val").val(result[0].cpId).text(result[0].productInfo.proName);
                $("#scVal").val(result[0].scVal);
                $("#scDate").val(result[0].scDate);
                $("#remark").val(result[0].remark);
            })
        }
    })
    let is_submit = false;
    let url = "";
    if (id !== null && id !== '') {
        url = '/py/updateProductYield';
    } else {
        url = '/py/addProductYield';
    }

    function addAndUpdateObject() {
        let bean = {
            id: id,
            cpId: $("#cpId").val(),
            scVal: $("#scVal").val(),
            scDate: $("#scDate").val(),
            remark: $("#remark").val(),
        }
        let submit = true;
        if (id ===null || id === ''){
          for (let beanKey in bean) {
            let beanElement = bean[beanKey];
            if ((beanElement === null || beanElement === "") && beanKey !== "id" && beanKey !== "remark") {
              submit = false;
              $("#" + beanKey).siblings(".cError").empty().append(`<span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>`);
              return;
            }
          }
        }
        if (submit) {
            if (top.ajax.synclBody(url, bean)) {
                modelWindow.alert(top.CMSLang.Add_success);
                top.getCurrentFrameObj().loadListble();
                top.tab_colseOnclick();
            } else {
                modelWindow.alert(top.CMSLang.Add_fail);
            }
        }
    }

    $("#cpId").click(function () {
        let $that = $(this);
        let url = '/pm/getObjByArgs';
        let children = $($that).children();
        $("#cpId_val").hide();
        if (children.length <= 1) {
            $.get(url, function (result) {
                result.forEach((item) => {
                    $($that).append(`
                    <option value="` + item.cpId + `">` + item.proName + `</option>
                `)
                })
            })
        }
    })

    $("#report").on('change', function () {
        let file = this.files[0];
        let name = file.name;
        let indexOf = name.lastIndexOf('.');
        let fileSuffix = name.slice(indexOf);
        let suffix = ['.doc', '.docx', '.pdf'];
        if (suffix.includes(fileSuffix)) {
            $("#report_label").css('color', '#615e57').text('已选择:' + name);
        } else {
            $("#input_label").attr('data-content', '请确认文件为 word或者pdf')
            $("#report_label").css('color', 'red').text('!文件格式错误!');
            $('#input_label').popover('show');
            $(this).val('');
        }
    })

</script>
</body>

</html>
